import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { config } from '../../shared/smartadmin.config'
import { Router, ActivatedRoute } from '@angular/router' //路由引入
declare var $: any
declare var echarts: any
@Component({
  selector: 'sa-data-repair-log',
  templateUrl: './data-repair-log.component.html',
  styleUrls: ['./data-repair-log.component.css']
})
export class DataRepairLogComponent implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  deviceId: string = '' //设备ID
  carNumber: string = '' //车牌号码
  startTime: string = '' //开始时间
  endTime: string = '' //结束时间
  haiLuoDeviceNo: string = '' // 海螺设备号
  orderCode: string = '' // 订单号
  selectedCompanyId: string = ''
  selectedCompanyName: string = ''
  activeSelected: string = ''
  pushState: string = ''
  pushTargetType: string = ''
  allChecked: boolean = false // 全选状态
  selected: any[] = [] // 选中的数据
  selectedRow: any
  searchKey: string = '' //搜索keyword

  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }

  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.startTime = time.startTime
    this.endTime = time.endTime
    this.load()
  }

  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
    this.deviceId = data.deviceId
    this.carNumber = data.carNumber
  }
  ngOnInit() {
    this.load()
  }
  //  初始化加载list数据
  load() {
    this.allChecked = false // 全选置空
    this.selected = []
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        startTime: this.startTime,
        endTime: this.endTime,
        haiLuoDeviceNo: this.haiLuoDeviceNo,
        orderCode: this.orderCode,
        pushState: this.pushState == '' ? null : this.pushState,
        pushTargetType: this.pushTargetType,
        deviceId: this.deviceId,
        companyId: this.selectedCompanyId,
        companyName: this.selectedCompanyName,
        carNumber: this.carNumber,
        searchKey: this.searchKey
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('haiLuoWaitPushLog/queryHaiLuoPushCarLogPage.html', reqdata).subscribe(data => {
      if (!data.isSuccess) {
        this.jsonApiService.message(data.message)
        return
      }
      this.tableData = data.exhibitDatas
      this.totalCount = data.recordCount
    })
  }
  ngOnDestroy() {
    document.onclick = null
  }
  //刷新
  refresh() {
    this.load()
  }
  //查询
  table_search() {
    this.curPage = 1
    this.searchKey = common.stringTrim(this.searchKey)
    this.load()
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }

  // 清空
  delKey(type) {
    if (type == 'orderCode') {
      this.orderCode = ''
    }
    if (type == 'haiLuoDeviceNo') {
      this.haiLuoDeviceNo = ''
    }
  }

  // 批量推送
  pushAll() {
    if (this.selected.length) {
      let reqdata = 'ids=' + this.selected.join(',')
      this.jsonApiService.http('haiLuoPushCarLog/pushRestartByIds.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    } else {
      this.jsonApiService.message('请勾选数据推送')
    }
  }
  //单独推送
  pushItem(row) {
    let selected = this.selected.length ? this.selected : [row.id]
    let reqdata = 'id=' + selected
    this.jsonApiService.http('haiLuoWaitPushLog/pushToHaiLuo.html', reqdata).subscribe(result => {
      this.jsonApiService.message(result.message)
      if (result.success) {
        this.load()
      }
    })
  }
}
